<template>
  <cp-nav-bar @click-right="clickRight" title="登陆" rightText="注册"></cp-nav-bar>
  <div class="login">
    <div class="login-head">
      <h3>{{ isPass ? '密码登录' : '短信验证码登录' }}</h3>
      <a href="javascript:;" @click="isPass = !isPass">
        <span>{{ !isPass ? '密码登录' : '短信验证码登录' }}</span>
        <van-icon name="arrow"></van-icon>
      </a>
    </div>

    <van-form autocomplete="off" @submit="login" ref="form">
      <van-field
        placeholder="请输入手机号"
        :rules="mobileRules"
        v-model="loginForm.mobile"
        type="tel"
        name="mobile"
      ></van-field>
      <van-field
        v-if="isPass"
        v-model="loginForm.password"
        placeholder="请输入密码"
        :rules="passwordRules"
        :type="show ? 'text' : 'password'"
        autocomplete="off"
      >
        <template #button>
          <cp-icon @click="show = !show" :name="`login-eye-${show ? 'on' : 'off'}`"></cp-icon>
        </template>
      </van-field>

      <van-field v-else placeholder="短信验证码" v-model="code" :rules="codeRules">
        <template #button>
          <button :disabled="disabled || sending" @click="sendVerificationCode">
            {{ sending ? '发送中...' : buttonText }}
          </button>
        </template>
      </van-field>
      <div class="cp-cell">
        <van-checkbox v-model="agree">
          <span>我已同意</span>
          <a href="javascript:;">用户协议</a>
          <span>及</span>
          <a href="javascript:;">隐私条款</a>
        </van-checkbox>
      </div>
      <div class="cp-cell">
        <van-button block round type="primary" native-type="submit">登 录</van-button>
      </div>
      <div class="cp-cell">
        <a href="javascript:;">忘记密码？</a>
      </div>
    </van-form>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { showFailToast, showSuccessToast, type FormInstance } from 'vant'
import { loginByPassword, loginByCode, sendMobileCode } from '@/api/user'
import { mobileRules, passwordRules, codeRules } from '@/utils/rules'
import { useUserStore } from '@/stores/modules/user'
import { useRouter, useRoute } from 'vue-router'
import { useVerificationCode } from '@/hooks/useSendCode'

const agree = ref(false) // 表单数据
const loginForm = reactive({ mobile: '', password: '' })
// 控制密码是否显示
const show = ref(false)
const store = useUserStore()
//跳转页面使用
const router = useRouter()
// 获取路由参数
const route = useRoute()
const form = ref<FormInstance>()
const code = ref('') //验证码
console.log('loginForm.mobile', loginForm.mobile)

//2、验证码登录
//====发送验证码=====
const isPass = ref(true) //true 密码登陆 | false 短信登录
const clickRight = () => {
  console.log('clickRight')
}

const login = async () => {
  console.log('loginForm.mobile1', loginForm.mobile)
  if (!agree.value) return showFailToast('请勾选我已同意')
  try {
    const res = isPass.value
      ? await loginByPassword(loginForm)
      : await loginByCode({ mobile: loginForm.mobile, code: code.value })
    console.log('res', res)
    store.setUser(res.data)
    router.push((route.query.returnUrl as string) || '/user')
    showSuccessToast('登陆成功')
  } catch (err) {
    console.log('err', err)
  }
}

const { disabled, sending, buttonText, sendVerificationCode } = useVerificationCode({
  countdown: 60,
  sendCode: async () => {
    //   //输入手机号时 对手机号进行验证
    await form.value?.validate('mobile')
    const { data } = await sendMobileCode({ mobile: loginForm.mobile, type: 'login' })
    console.log(data)
    code.value = data.code
    console.log('data', data)
  },
})
</script>
<style scoped lang="scss">
.login {
  padding-top: 46px;
  &-head {
    padding: 30px 30px 50px;
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    line-height: 1;
    h3 {
      font-weight: normal;
      font-size: 24px;
    }
    a {
      font-size: 15px;
    }
  }
  .van-form {
    padding: 0 14px;
    .cp-cell {
      height: 52px;
      padding: 14px 16px;
      box-sizing: border-box;
      display: flex;
      align-items: center;
      .van-checkbox {
        a {
          color: var(--cp-primary);
          padding: 0 5px;
        }
      }
    }
    .active {
      color: red;
    }
  }
}
</style>
